package com.lkd.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lkd.dto.VmPageDto;
import com.lkd.entity.Business;
import com.lkd.entity.Region;
import com.lkd.entity.VmType;
import com.lkd.mapper.*;
import com.lkd.result.PageBean;
import com.lkd.service.VmService;
import com.lkd.vo.NodePageVo;
import com.lkd.vo.VmPageVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class VmServiceImpl implements VmService {
    private final VmMapper vmMapper;

    private final VmTypeMapper vmTypeMapper;

    private final RegionMapper regionMapper;

    private final NodeMapper nodeMapper;

    private final BusinessMapper businessMapper;
    //设备搜索
    @Override
    public PageBean search(VmPageDto vmPageDto) {
        PageHelper.startPage(vmPageDto.getPageIndex(),vmPageDto.getPageSize());
        List<VmPageVo> list = vmMapper.search(vmPageDto);

        list.forEach(vmPageVo -> {
            //通过查询出的vmtype 查出type表 放入vo中
            VmType type = vmTypeMapper.getByVmType(vmPageVo.getVmType());
            vmPageVo.setType(type);

            //通过查询出的region id 查出region 表放入vo中
            Region region = regionMapper.getByRid(Integer.parseInt(vmPageVo.getRegionId()));
            vmPageVo.setRegion(region);

            //通过查询出的nodeid 查出nodepagevo返回
            String nodeId = vmPageVo.getNodeId();
            NodePageVo nodePageVo = nodeMapper.getByNodeId(Integer.parseInt(nodeId));
            //根据该点位id查询出 region businesstype 放入nodevo中
            Region region1 = regionMapper.getByRid(Integer.parseInt(nodePageVo.getId()));
            nodePageVo.setRegion(region1);
            Business business = businessMapper.getById(nodePageVo.getBusinessId());
            nodePageVo.setBusinessType(business);
            //再把nodepagevo放入vo中
            vmPageVo.setNode(nodePageVo);
        });

        Page<VmPageVo> page = (Page<VmPageVo>) list;
        return new PageBean<>(page.getPageNum(),page.getPageSize(), (long) page.getPages(),page.getTotal(),page.getResult());

    }
}

